// InitAPIThread.cpp : implementation file
//

#include "stdafx.h"
#include "signalanalyzer.h"
#include "SHAPIThread.h"
#include "SignalAnalyzerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSHAPIThread

IMPLEMENT_DYNCREATE(CSHAPIThread, CWinThread)

CSHAPIThread::CSHAPIThread()
{
}

CSHAPIThread::~CSHAPIThread()
{
}

BOOL CSHAPIThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
//	m_dlg.Create(IDD_INIT_SHAPI_DIALOG, NULL);
//	m_dlg.ShowWindow(SW_SHOW);
	m_socket.Create(0, SOCK_DGRAM);
	return TRUE;
}

int CSHAPIThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
//	m_dlg.DestroyWindow();
	m_socket.Close();
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CSHAPIThread, CWinThread)
	//{{AFX_MSG_MAP(CSHAPIThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
	ON_THREAD_MESSAGE(WM_INIT_API, OnInitAPI)
	ON_THREAD_MESSAGE(WM_CONFIG_API, OnConfigAPI)
	ON_THREAD_MESSAGE(WM_SWEEPCOUNT_API, OnSweepCountAPI)
	ON_THREAD_MESSAGE(WM_SWEEP_API, OnSweepAPI)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSHAPIThread message handlers

void CSHAPIThread::OnInitAPI(UINT wParam, LONG lParam){
	int retval = SHAPI_Initialize();
	if (retval == 0){
		SHAPI_SetPreamp(1);
	}
	PostMessage(GetMainWnd()->GetSafeHwnd(), WM_INIT_API_RESULT, retval, NULL);
}

void CSHAPIThread::OnConfigAPI(UINT wParam, LONG lParam){
	double attenVal = 0;
	int mixerBand = 0;
	int sensitivity = 2;
	int decimation = 1;
	int IF_Path = 0;
	int ADCclock = 0;
	int retval = SHAPI_Configure(attenVal,mixerBand,sensitivity,decimation,IF_Path,ADCclock);
	PostMessage(GetMainWnd()->GetSafeHwnd(), WM_CONFIG_API_RESULT, retval, NULL);
}

void CSHAPIThread::OnSweepCountAPI(WPARAM wParam,LPARAM lParam){
	SweepCountAPIParam* pSweepCountAPIParam = (SweepCountAPIParam*)wParam;
	int retval = SHAPI_GetFastSweepCount(pSweepCountAPIParam->m_startFreq,pSweepCountAPIParam->m_stopFreq,pSweepCountAPIParam->m_fftsz);
	delete pSweepCountAPIParam;
	PostMessage(GetMainWnd()->GetSafeHwnd(), WM_SWEEPCOUNT_API_RESULT, retval, NULL);
}

void CSHAPIThread::OnSweepAPI(WPARAM wParam,LPARAM lParam){
	SweepAPIParam* pSweepAPIParam = (SweepAPIParam*)wParam;
	SHAPI_GetFastSweep(pSweepAPIParam->m_yPlotData, pSweepAPIParam->m_startFreq, pSweepAPIParam->m_stopFreq, pSweepAPIParam->m_sweepsize, pSweepAPIParam->m_fftsz);
	////for test///SendData(pSweepAPIParam->m_xPlotData, pSweepAPIParam->m_yPlotData, pSweepAPIParam->m_sweepsize);
	PostMessage(GetMainWnd()->GetSafeHwnd(), WM_SWEEP_API_RESULT, (WPARAM)pSweepAPIParam, NULL);

	/*
	for (int i = 0; i < m_sweepsize; i++){
		m_yPlotData[i] = rand()*(0-(-100))/RAND_MAX+(-100);
	}
	*/
	//-103.06224825841218
	//-93.415214321756395
	//-90.356418991278218
}

void CSHAPIThread::SendData(double *pX, double *pY, long nNum){
	/*
	CString sendData;
	CTime time=CTime::GetCurrentTime();
	sendData = time.Format("%Y-%m-%d %H:%M:%S");
	sendData.Format(sendData + "|%ld", nNum);
	CString data;
	for (long i = 0; i < nNum; i++){
		data.Format("|%lf,%lf", pX[i], pY[i]);
		sendData += data;
	}
	m_socket.SendTo(sendData, sendData.GetLength(), 8888, "127.0.0.1");
	*/
	
	for (int i = 1; i <= nNum; i++){
		SHPackage data;
		data.id = i;
		data.x = pX[i];
		data.y = pY[i];
		if (i < nNum){
			data.endFlag = FALSE;
		}else{
			data.endFlag = TRUE;
		}
		m_socket.SendTo(&data, sizeof(data), 8888, "127.0.0.1");
	}

}
